﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace EasyNet.Componets.Core.Algorithms
{
    /// <summary>
    ///     RSA加密解密及RSA签名和验证
    /// </summary>
    public sealed class SHA
    {
        private static SHA _instance;
        /// <summary>
        /// 单一实例
        /// </summary>
        public static SHA Instance
        {
            get
            {
                return _instance = _instance ?? new SHA();
            }
        }

        #region SHA1加密
        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string CreateSha1(string input, Encoding encoding = null)
        {
            //建立SHA1对象
            SHA1 sha = new SHA1CryptoServiceProvider();
            //将mystr转换成byte[]
            encoding = encoding ?? Encoding.UTF8;
            byte[] dataToHash = encoding.GetBytes(input);
            //Hash运算
            byte[] dataHashed = sha.ComputeHash(dataToHash);
            //将运算结果转换成string
            string hash = BitConverter.ToString(dataHashed);

            return hash;
        }
        #endregion

    }
}
